package cn.sherer.utils;

/**
 * 运算工具类
 */
public class OperatorUtils {

    // <editor-fold defaultstate="collapsed" desc="枚举类型">
    /**
     * 运算符类型
     */
    public enum Operator {
        EQUAL, NOT_EQUAL, BIG, BIG_EQUAL, SMALL, SMALL_EQUAL
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="逻辑运算符——或">
    /**
     * 逻辑运算符 或
     * @param source 比较对象
     * @param target 被比较对象
     * @return 比较结果
     */
    public static boolean OR(Object source, Object... target) {
        return OR(source, Operator.EQUAL, target);
    }

    /**
     * 逻辑运算符 或
     * @param source 比较对象
     * @param operator 运算符类型
     * @param target 被比较对象
     * @return 比较结果
     */
    public static boolean OR(Object source, Operator operator, Object... target) {
        switch (operator) {
            case EQUAL: {
                for (Object object : target) {
                    if (source==object || String.valueOf(object).equals(source)) { return true; }
                }
                break;
            }
            case NOT_EQUAL: {
                for (Object object : target) {
                    if (source!=object && !String.valueOf(object).equals(source)) { return true; }
                }
                break;
            }
            case BIG: {
                String sourceValue = String.valueOf(source);
                if (!StringUtils.isNumeric(sourceValue)) break;
                for (Object object : target) {
                    String targetValue = String.valueOf(object);
                    if (!StringUtils.isNumeric(targetValue)) continue;
                    if (Double.valueOf(sourceValue) > Double.valueOf(targetValue)) { return true; }
                }
                break;
            }
            case BIG_EQUAL: {
                String sourceValue = String.valueOf(source);
                if (!StringUtils.isNumeric(sourceValue)) break;
                for (Object object : target) {
                    String targetValue = String.valueOf(object);
                    if (!StringUtils.isNumeric(targetValue)) continue;
                    if (Double.valueOf(sourceValue) >= Double.valueOf(targetValue)) { return true; }
                }
                break;
            }
            case SMALL: {
                String sourceValue = String.valueOf(source);
                if (!StringUtils.isNumeric(sourceValue)) break;
                for (Object object : target) {
                    String targetValue = String.valueOf(object);
                    if (!StringUtils.isNumeric(targetValue)) continue;
                    if (Double.valueOf(sourceValue) < Double.valueOf(targetValue)) { return true; }
                }
                break;
            }
            case SMALL_EQUAL: {
                String sourceValue = String.valueOf(source);
                if (!StringUtils.isNumeric(sourceValue)) break;
                for (Object object : target) {
                    String targetValue = String.valueOf(object);
                    if (!StringUtils.isNumeric(targetValue)) continue;
                    if (Double.valueOf(sourceValue) <= Double.valueOf(targetValue)) { return true; }
                }
                break;
            }
        }
        return false;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="逻辑运算符——与">
    /**
     * 逻辑运算符 与
     * @param source 比较对象
     * @param target 被比较对象
     * @return 比较结果
     */
    public static boolean AND(Object source, Object... target) {
        return AND(source, Operator.EQUAL, target);
    }

    /**
     * 逻辑运算符 与
     * @param source 比较对象
     * @param operator 运算符类型
     * @param target 被比较对象
     * @return 比较结果
     */
    public static boolean AND(Object source, Operator operator, Object... target) {
        switch(operator) {
            case EQUAL: {
                for (Object object : target) {
                    if (source!=object && !String.valueOf(object).equals(source)) { return false; }
                }
                break;
            }
            case NOT_EQUAL: {
                for (Object object : target) {
                    if (source==object || String.valueOf(object).equals(source)) { return false; }
                }
                break;
            }
            case BIG: {
                String sourceValue = String.valueOf(source);
                if (!StringUtils.isNumeric(sourceValue)) break;
                for (Object object : target) {
                    String targetValue = String.valueOf(object);
                    if (!StringUtils.isNumeric(targetValue)) continue;
                    if (Double.valueOf(sourceValue) <= Double.valueOf(targetValue)) { return false; }
                }
                break;
            }
            case BIG_EQUAL: {
                String sourceValue = String.valueOf(source);
                if (!StringUtils.isNumeric(sourceValue)) break;
                for (Object object : target) {
                    String targetValue = String.valueOf(object);
                    if (!StringUtils.isNumeric(targetValue)) continue;
                    if (Double.valueOf(sourceValue) < Double.valueOf(targetValue)) { return false; }
                }
                break;
            }
            case SMALL: {
                String sourceValue = String.valueOf(source);
                if (!StringUtils.isNumeric(sourceValue)) break;
                for (Object object : target) {
                    String targetValue = String.valueOf(object);
                    if (!StringUtils.isNumeric(targetValue)) continue;
                    if (Double.valueOf(sourceValue) >= Double.valueOf(targetValue)) { return false; }
                }
                break;
            }
            case SMALL_EQUAL: {
                String sourceValue = String.valueOf(source);
                if (!StringUtils.isNumeric(sourceValue)) break;
                for (Object object : target) {
                    String targetValue = String.valueOf(object);
                    if (!StringUtils.isNumeric(targetValue)) continue;
                    if (Double.valueOf(sourceValue) > Double.valueOf(targetValue)) { return false; }
                }
                break;
            }
        }
        return true;
    }
    // </editor-fold>

}
